
export const quitLogin = function() {
	// uni.showToast({
	// 	title: '出错了，本该退出，为避免账号又要配合登录暂且不退',
	// 	icon: 'none'
	// })
	uni.removeStorageSync('token');
	uni.removeStorageSync('userId');
	uni.removeStorageSync('phone');
	uni.removeStorageSync('userInfo');
	uni.removeStorageSync('beCompleteInfo');
	uni.reLaunch({
		url: '/pages/sign_in/sign_in'
	})
}


/**
 *
 * @param {缓存key} key
 * @param {需要存储的缓存值} value
 * @param {过期时间，默认0表示永久有效} expire
 */
export const setCache = (key, value, expire = 0) => {
	let obj = {
		data: value, //存储的数据
		time: Date.now() / 1000, //记录存储的时间戳
		expire: expire //记录过期时间，单位秒
	}
	uni.setStorageSync(key, JSON.stringify(obj))
}


/**
 *
 * @param {缓存key} key
 */
export const getCache = (key) => {
	let val = uni.getStorageSync(key)
	if (!val) {
		return null
	}
	val = JSON.parse(val)
	if (val.expire && Date.now() / 1000 - val.time > val.expire) {
		uni.removeStorageSync(key)
		return null
	}
	return val.data
}


export const getParam = (path, name) => {

	var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");

	if (reg.test(path)) {
		return unescape(RegExp.$2.replace(/\+/g, " "));
	}
	return "";

}

export const goBack = () => {
	let pages = getCurrentPages(); // 当前页面
	let arr = [
		'/pages/index/index',
		'/pages/index/product',
		'/pages/index/agent',
		'/pages/index/company',
		'/pages/index/cooperation',
		'/pages/new_live/new_live',
		'/pages/Product_list/Product_list',
		'/pages/jingxuan/list',
		'/pages/jingxuan/entryPageList',
		'/pagesA/new_selected_theme_shopping_mode/new_selected_theme_shopping_mode',
		'/pagesA/Leading_brand/Leading_brand'
	];

	if (pages.length > 2) {
		let route = '/' + pages[pages.length - 2].route
		console.log('route', route)
		console.log('pages', pages)
		if (arr.includes(route)) {
			uni.redirectTo({
				url: route
			})
		} else {
			uni.navigateBack({
				delta: 1
			})
		}
	} else {
		uni.redirectTo({
			url: '/pages/index/index'
		})
	}
}

export const goHome = () => {
	uni.redirectTo({
		url: '/pages/index/index'
	})
}


export const getTodayDate = () => {
	let data = new Date();
	let year = data.getFullYear();
	let month = data.getMonth() + 1;
	let day = data.getDate();
	let h = data.getHours();
	let mm = data.getMinutes();
	let s = data.getSeconds();
	month = month > 10 ? month : "0" + month;
	day = day > 10 ? day : "0" + day;
	//h = h > 10 ? h : "0" + h;
	mm = mm > 10 ? mm : "0" + mm;
	s = s > 10 ? s : "0" + s;
	return `${year}-${month}-${day}`;
}



